<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>文章列表</title>
    <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css" />
    <link rel="stylesheet" href="css/reset.css" />
    <link rel="stylesheet" href="css/iconfont.css" />
    <link rel="stylesheet" href="css/main.css" />
    <script src="./libs/jquery-1.12.4.min.js"></script>
    <script src="./libs/bootstrap/js/bootstrap.min.js"></script>
    <!-- 引入模板引擎js文件 -->
    <script src="./libs/template-web.js"></script>
  </head>

  <body>
    <div class="container-fluid">
      <div class="common_title">
        文章类别管理
      </div>
      <div class="container-fluid common_con">
        <table
          class="table table-striped table-bordered table-hover mp20 category_table"
        >
          <thead>
            <tr>
              <th>名称</th>
              <th>Slug</th>
              <th class="text-center" width="100">操作</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>奇趣事</td>
              <td>funny</td>
              <td class="text-center">
                <a
                  href="javascript:void(0)"
                  data-toggle="modal"
                  class=" btn btn-info btn-xs"
                  >编辑</a
                >
                <a href="javascript:void(0)" class="btn btn-danger btn-xs"
                  >删除</a
                >
              </td>
            </tr>
          </tbody>
          <tfoot>
            <tr>
              <td colspan="3" class="text-center">
                <a
                  href="javascript:void(0)"
                  class="btn btn-success"
                  id="xinzengfenlei"
                  data-toggle="modal"
                  data-target="#myModal"
                  >新增分类</a
                >
              </td>
            </tr>
          </tfoot>
        </table>
      </div>
    </div>

    <!-- 模态输入框结构 -->
    <div
      class="modal fade"
      id="myModal"
      tabindex="-1"
      role="dialog"
      aria-labelledby="exampleModalLabel"
    >
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button
              type="button"
              class="close"
              data-dismiss="modal"
              aria-label="Close"
            >
              <span aria-hidden="true">&times;</span>
            </button>
            <h4 class="modal-title" id="exampleModalLabel">New message</h4>
          </div>
          <div class="modal-body">
            <form>
              <div class="form-group">
                <label for="recipient-name" class="control-label"
                  >类别名称</label
                >
                <input type="text" class="form-control" id="recipient-name" />
              </div>
              <div class="form-group">
                <label for="message-text" class="control-label">别名</label>
                <input class="form-control" id="message-text" />
              </div>
            </form>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">
              关闭
            </button>
            <button type="button" class="btn btn-primary btn-change">
              Send message
            </button>
          </div>
        </div>
      </div>
    </div>

    <!-- 模态提示框 -->
    <div class="modal fade" tabindex="-1" role="dialog" id="myModal1">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button
              type="button"
              class="close"
              data-dismiss="modal"
              aria-label="Close"
            >
              <span aria-hidden="true">&times;</span>
            </button>
            <h4 class="modal-title">温馨提示</h4>
          </div>
          <div class="modal-body">
            <p>One fine body&hellip;</p>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">
              确认
            </button>
          </div>
        </div>
        <!-- /.modal-content -->
      </div>
      <!-- /.modal-dialog -->
    </div>
    <!-- /.modal -->
  </body>
</html>

<script src="./libs/https.js"></script>
<!-- 2. 模板引擎 -->
<script type="text/html" id="tpl_category">
  {{each}}
  <tr>
  <td>{{$value.name}}</td>
  <td>{{$value.slug}}</td>
  <td class="text-center">
    <a
      href="javascript:void(0)"
      data-toggle="modal"
      data-target="#myModal"
      class=" btn btn-info btn-xs"
      dataid="{{$value.id}}"
      >编辑</a
    >
    <a href="javascript:void(0)" class="btn btn-danger btn-xs btn-del" dataid="{{$value.id}}"
      >删除</a
    >
  </td>
  </tr>
  {{/each}}
</script>
<script>
  $(function () {
    // 1. 发送ajax请求 获取文章类别管理
    // 3.2 封装函数
    function ajax_list () {
      $.ajax({
        type: 'get',
        url: BigNew.category_list,
        success: function (backData) {
          // console.log(backData)
          if (backData.code == 200) {
            let data = backData.data
            let htmlStr = template('tpl_category', data)
            // console.log(htmlStr)
            $('tbody').html(htmlStr)
          }
        }
      })
    }

    // 3.3 自调用
    ajax_list()
    // 3.1 储存编辑，新增所需参数和地址
    let data = {}
    let url = ''

    // 2. 模态框来源区分
    $('#myModal').on('show.bs.modal', function (e) {
      // console.log(e.relatedTarget)
      // 2.1 判断标签
      if ($(e.relatedTarget).text() == '新增分类') {
        $('#exampleModalLabel').text('新增分类')
        $('.btn-change').text('新增')
        // 2.3 清除输入框的内容
        $('#recipient-name').val('')
        $('#message-text').val('')
        url = BigNew.category_add
        // console.log(url)
        data.id = ''
        // console.log(data)
      } else {
        $('#exampleModalLabel').text('编辑分类')
        $('.btn-change').text('编辑')

        // 2.2 获取对应标签的文本
        let name = $(e.relatedTarget)
          .parents()
          .eq(1)
          .children()
          .eq(0)
          .text()
        let slug = $(e.relatedTarget)
          .parents()
          .eq(1)
          .children()
          .eq(1)
          .text()
        $('#recipient-name').val(name)
        $('#message-text').val(slug)

        // 2.3 获取a标签中的id
        let id = $(e.relatedTarget).attr('dataid')
        data.id = id
        url = BigNew.category_edit
        // console.log(data)
      }
    })

    // 3. 点击按钮，发送ajax请求
    $('.btn-primary').on('click', function () {
      let name = $('#recipient-name')
        .val()
        .trim()
      let slug = $('#message-text')
        .val()
        .trim()
      data.name = name
      data.slug = slug
      // 3.5 判断输入框不为空
      if (name == '' || slug == '') {
        // 3.6 模态框隐藏方法
        // $('#myModal').modal('show')
        // alert('内容不能为空')
        $('#myModal1').modal()
        return
      }
      // console.log(data)
      $.ajax({
        type: 'post',
        url: url,
        data: data,
        success: function (backData) {
          // console.log(backData)
          // alert(backData.msg)
          if (backData.code >= 200 && backData.code < 300) {
            $('#myModal1 p').text(backData.msg)
            $('#myModal1').modal()
            $('#myModal1').on('hidden.bs.modal', function () {
              ajax_list()
            })
            // 3.7 成功隐藏
            $('#myModal').modal('hide')
          }

          // 3.4 调用ajax列表请求
        }
      })
    })

    // 4. 文章分类删除功能
    $('tbody').on('click', '.btn-del', function () {
      let dataid = $(this).attr('dataid')
      // console.log(dataid)
      $.ajax({
        type: 'post',
        url: BigNew.category_delete,
        data: {
          id: dataid
        },
        success: function (backData) {
          // console.log(backData)
          // 4.1 判断是否成功
          if (backData.code == 204) {
            $('#myModal1 p').text(backData.msg)
            $('#myModal1').modal()
            $('#myModal1').on('hidden.bs.modal', function () {
              ajax_list()
            })
          }
        }
      })
    })
  })
</script>
